package com.qingke.easyjava.calculator;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** match匹配器。
 * 	matches 方法尝试将整个输入序列与该模式匹配。 

	lookingAt 尝试将输入序列从头开始与该模式匹配。 

	find 方法扫描输入序列以查找与该模式匹配的下一个子序列。 

 * @author qingke
 *
 */
public class CalculatorApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("请输入运算公式: ");
        String line = scanner.nextLine();
        Pattern pattern = Pattern.compile("^(-?\\d+(?:\\.\\d+)?)([/+-/*//])(\\d+(?:\\.\\d+)?)$");
        Matcher m = pattern.matcher(line);
        
        if (m.find()) {
            String str1 = m.group(1);
            String operator = m.group(2);
            String str2 = m.group(3);
            
            System.out.println("str1: " + str1 + ", operator: " + operator + ", str2: " + str2);
           
            Operator opt = new OperatorFactory().buildOperator(operator);
            
            double d1 = Double.parseDouble(str1);
            double d2 = Double.parseDouble(str2);
            
            double result;
            try {
                result = opt.doOperate(d1, d2);
                System.out.println("result: " + result);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }
    }

}












